|
STEP 15: Creating Scriptable Instances
|
While we've shown in the previous step that using Music Object's Music.magicEmbed static method can produce cleaner cross-browser code than the combined EMBED-OBJECT tag approach, we still don't get the benefits of scriptable interactivity provided by the Music Object API. In order to be able to control the Player instance after it has been created, we need to first create a Music Object instance and then use that instance's magicEmbed method instead of the Music.magicEmbed static method.
Take a look at the before-and-after comparison below to see how we've taken the code from the previous step in this tutorial and translated it to use the magicEmbed instance method in place of the Music.magicEmbed static method...
| BEFORE (Using the Music.magicEmbed Static Method) |
<SCRIPT SRC="music-object.js"></SCRIPT> <SCRIPT LANGUAGE=JavaScript><!-- // Music.magicEmbed ( 'SRC="trance.rmf" ' + 'WIDTH=144 ' + 'HEIGHT=60 ' + 'DISPLAY=SONG ' + 'AUTOSTART=FALSE ' + 'LOOP=FALSE' ); // --></SCRIPT> |
| AFTER (Using the magicEmbed Instance Method) |
<SCRIPT SRC="music-object.js"></SCRIPT> <SCRIPT LANGUAGE=JavaScript><!-- // myMusicObject = new Music (); myMusicObject.magicEmbed ( 'SRC="trance.rmf" ' + 'WIDTH=144 ' + 'HEIGHT=60 ' + 'DISPLAY=SONG ' + 'AUTOSTART=FALSE ' + 'LOOP=FALSE' ); // --></SCRIPT> |
The text that is highlighted represents text that has been added or changed in order to use the instance method instead of the static method. You will notice how we've created a new Music Object instance in the statement
Of course, we still don't have any actual scripting for interactive control of the Player instance - we've just set up the foundation to allow the Player instance to be controlled through the Music Object instance "myMusicObject". In the following source code listing we've added some more HTML code to control the playback of the Player instance from HTML form buttons. This is quite an unimaginitive example of interactive music, but it does serve to illustrate the principle...
<SCRIPT SRC="music-object.js"></SCRIPT>
<CENTER>
<SCRIPT LANGUAGE=JavaScript><!-- //
myMusicObject = new Music ();
myMusicObject.magicEmbed (
'SRC="trance.rmf" ' +
'WIDTH=144 ' +
'HEIGHT=60 ' +
'DISPLAY=SONG ' +
'AUTOSTART=FALSE ' +
'LOOP=FALSE'
);
// --></SCRIPT>
<FORM>
<INPUT TYPE=button VALUE="PLAY" ONCLICK="myMusicObject.play ()">
<INPUT TYPE=button VALUE="PAUSE" ONCLICK="myMusicObject.pause ()">
<INPUT TYPE=button VALUE="STOP" ONCLICK="myMusicObject.stop (2000)">
</FORM>
</CENTER>
|
The text that is highlighted indicates the text that has been added to achieve a very token level of musical interactivity. A simple form containing three buttons lets the user control the play, pause, and stop functions of the Player instance that is embedded right above the buttons. Of course, with a more ambitious and more imaginitive Web site sonification, it could be any events within a page that controls a Player instance. Notice how each INPUT tag has an ONCLICK event handler, each calling a different method of the Music Object instance "myMusicObject".
Now, below you will see the results that this HTML code generates. Click the buttons and see how it works...